<template>
	<view class="wrap">
		<view class="top">
			<view class="search">
				<input type="text" v-model="keywords" placeholder="请输入工人姓名">
				<uni-icons @click="init" type="search" color="#bababa" size="20"></uni-icons>
			</view>
			<view class="manage" @click="isManage=!isManage">
				<text v-if="!isManage">管理</text>
				<text v-else>取消</text>
			</view>
		</view>
		<view class="list">
			<checkbox-group @change="checkboxChange">
				<view class="col" v-for="item in list" :key="item.work_no" @click="goDetail(item)">
					<checkbox style="transform: scale(0.9);" v-if="isManage" :value="item.work_no" :checked="item.checked" />
					<image :src="item.avatar" mode=""></image>
					<view class="text" :class="{'isManage':isManage}">
						<view class="name">
							{{item.nickname}}
						</view>
						<view class="time">
							最近登录:
							<text v-if="item.prevtime">
								{{item.prevtime}}
							</text>
							<text v-else> 无</text>
						</view>
					</view>
					<view class="post">
						{{item.post_name}}
					</view>
				</view>
			</checkbox-group>
		</view>
		<uni-load-more :status="status"></uni-load-more>
		<view class="bottom" v-if="isManage">
			<checkbox-group @change="changeAll">
				<label>
					<checkbox :checked="isAll" />全选
				</label>
			</checkbox-group>
			<view class="btn" @click="delStaff">
				<image src="@/static/admin/icon/del.png" mode=""></image>
				删除
			</view>
		</view>
		<view class="seat" v-if="isManage">

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				page: 1,
				isManage: false,
				isAll: false,
				status: "no-more",
				keywords: ''
			}
		},
		onShow() {
			this.init()
		},
		onPullDownRefresh() {
			this.init()
		},
		onReachBottom() {
			this.page++
			this.getList()
		},
		methods: {
			init() {
				this.list = []
				this.page = 1
				this.getList()
			},
			getList() {
				this.status = "loading"
				this.$post("/worker/worker_data", {
					page: this.page,
					keywords: this.keywords
				}).then(res => {
					this.status = "no-more"
					uni.stopPullDownRefresh()
					res.data.data.forEach(item => {
						item.checked = false
						this.list.push(item)
					})
				})
			},
			goDetail(item) {
				uni.navigateTo({
					url: "./detail?work_no=" + item.work_no
				})
			},
			checkboxChange(e) {
				var values = e.detail.value;
				this.list.forEach(item => {
					if (values.includes(item.work_no)) {
						this.$set(item, 'checked', true)
					} else {
						this.$set(item, 'checked', false)
					}
				})
				this.isAll = this.list.every(item => item.checked)
			},
			changeAll() {
				this.isAll = !this.isAll
				this.list.forEach(item => {
					item.checked = this.isAll
				})
			},
			delStaff() {
				let arr = []
				this.list.forEach(item => {
					if (item.checked) {
						arr.push(item.work_no)
					}
				})
				const work_no = arr.join(',')
				this.$post("/worker/worker_del", {
					work_no: work_no
				}).then(res => {
					if (res.code == 200) this.init()
				})
			}
		}
	}
</script>

<style lang="scss">
	.top {
		padding: 40rpx 0;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.search {
			width: 580rpx;
			height: 60rpx;
			line-height: 60rpx;
			background: #FFFFFF;
			border: 1rpx solid #E1E1E1;
			border-radius: 30px;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 30rpx;

			input {
				font-size: 28rpx;
				width: calc(100% - 40rpx);
			}
		}

		.manage {
			color: $main-color;
		}
	}

	.list {
		.col {
			padding: 24rpx 0;
			border-bottom: 1rpx solid #e2e2e2;
			display: flex;
			align-items: center;
			justify-content: space-between;

			image {
				width: 100rpx;
				height: 100rpx;
				border-radius: 50%;
			}

			.text {
				width: calc(100% - 250rpx);

				.name {
					font-size: 30rpx;
					margin-bottom: 16rpx;
				}

				.time {
					font-size: 24rpx;
					color: #797979;
				}
			}

			.isManage {
				width: calc(100% - 310rpx);
			}

			.post {
				max-width: 150rpx;
				color: #fff;
				background-color: $main-color;
				line-height: 32rpx;
				font-size: 22rpx;
				border-radius: 50px;
				padding: 0 16rpx;
			}
		}
	}

	/* ifdef MP-WEIXIN */
	/*checkbox选中后样式  */
	checkbox .wx-checkbox-input.wx-checkbox-input-checked {
		border: 2rpx solid $main-color !important;
		background-color: $main-color !important;
		color: #FFFFFF !important;
	}

	/*checkbox选中后图标样式  */
	checkbox .wx-checkbox-input.wx-checkbox-input-checked::before {
		color: #FFFFFF !important;
	}

	/* endif */

	.bottom {
		position: fixed;
		width: 100%;
		background-color: #fff;
		height: 100rpx;
		padding: 0 40rpx;
		bottom: 0;
		left: 0;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.btn {
			display: flex;
			width: calc(100% - 150rpx);
			color: #ff0000;
			line-height: 76rpx;
			background: #FFFFFF;
			border: 1rpx solid #CFCFCF;
			border-radius: 16rpx;
			align-items: center;
			justify-content: center;

			image {
				width: 36rpx;
				height: 36rpx;
				margin-right: 15rpx;
			}
		}
	}

	.seat {
		height: 100rpx;
	}
</style>